package com.evolution.warlock.dao.impl;  

import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.evolution.warlock.dao.MasterConfigDao;
import com.evolution.warlock.entity.MasterConfig;


import org.springframework.stereotype.Repository;
  
/**
 * DAO - 配置表
 * 
 */
@Repository("masterConfigDaoImpl")
public class MasterConfigDaoImpl extends BaseDaoImpl<MasterConfig, Long> implements MasterConfigDao{

  public MasterConfig find(String keyType, String cd) {
   CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
   CriteriaQuery<MasterConfig> criteriaQuery = criteriaBuilder.createQuery(MasterConfig.class);
   Root<MasterConfig> root = criteriaQuery.from(MasterConfig.class);
   criteriaQuery.select(root);
   Predicate restrictions = criteriaBuilder.conjunction();
   if (keyType != null) {
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("keyType"), keyType));
   }
   restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("cd"), cd));
   criteriaQuery.where(restrictions);
   criteriaQuery.orderBy(criteriaBuilder.asc(root.get("cd")));
   return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList().get(0);
  }
    
  public List<MasterConfig> findList(String keyType) {
   CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
   CriteriaQuery<MasterConfig> criteriaQuery = criteriaBuilder.createQuery(MasterConfig.class);
   Root<MasterConfig> root = criteriaQuery.from(MasterConfig.class);
   criteriaQuery.select(root);
   if (keyType != null) {
    criteriaQuery.where(criteriaBuilder.equal(root.get("keyType"), keyType));
   }
   criteriaQuery.orderBy(criteriaBuilder.asc(root.get("cd")));
   return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
  }

}
